package com.lqd.mall.system.controller;

import com.lqd.mall.basis.response.CommonResult;
import com.lqd.mall.basis.response.Result;
import com.lqd.mall.basis.utils.poi.ExcelUtil;
import com.lqd.mall.interaction.basis.vo.TableDataInfoVo;
import com.lqd.mall.interaction.system.SysLogininforEntity;
import com.lqd.mall.log.annotation.Log;
import com.lqd.mall.log.enums.BusinessType;
import com.lqd.mall.security.annotation.InnerAuth;
import com.lqd.mall.security.annotation.RequiresPermissions;
import com.lqd.mall.system.SysLogininforControllerApi;
import com.lqd.mall.system.service.ISysLogininforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Optional;

/**
 * @author 零起点
 * @create 2022-02-13 12:18
 */
@RestController
public class SysLogininforController implements SysLogininforControllerApi {

    @Autowired
    private ISysLogininforService sysLogininforService;


    @Override
    @InnerAuth
    public CommonResult saveLogininfor(SysLogininforEntity sysLogininforEntity) {
        int result = sysLogininforService.saveLogininfor(sysLogininforEntity);
        return result > 0 ? CommonResult.SUCCESS() : CommonResult.FAIL();
    }

    @Override
    public Result<TableDataInfoVo<SysLogininforEntity>> list(SysLogininforEntity sysLogininforEntity) {
        TableDataInfoVo<SysLogininforEntity> infoVo = sysLogininforService.list(sysLogininforEntity);
        return Result.SUCCESS(infoVo);
    }

    @Override
    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "删除登录日志", businessType = BusinessType.DELETE)
    public CommonResult deleteSysLoginfoById(Long infoId) {
        SysLogininforEntity entity = new SysLogininforEntity().setInfoId(infoId);
        Optional<Integer> integer = sysLogininforService.delectEntity(entity);
        return integer.get() > 0 ? CommonResult.SUCCESS() : CommonResult.FAIL();
    }

    @Override
    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志的清空操作", businessType = BusinessType.DELETE)
    public CommonResult deleteSysLoginfoAll() {
        Optional<Integer> integer = sysLogininforService.delectEntity(new SysLogininforEntity());
        return integer.get() > 0 ? CommonResult.SUCCESS() : CommonResult.FAIL();
    }

    @Override
    @Log(title = "登录日志的导出",businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:logininfor:export")
    public void export(HttpServletResponse response, SysLogininforEntity entity) {
        TableDataInfoVo<SysLogininforEntity> list = sysLogininforService.list(entity);
        List<SysLogininforEntity> dataAll = list.getRows();
        ExcelUtil<SysLogininforEntity> util = new ExcelUtil<SysLogininforEntity>(SysLogininforEntity.class);
        util.exportExcel(response, dataAll, "登录日志的导出");

    }
}
